// @Author EthanScriptOn
// @Desc
package response

type CommonResponse struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
}
type GeneratingFun func(...ResponseOption) *CommonResponse
type ResponseOption func(hook interface{})

func Success(hooks ...ResponseOption) *CommonResponse {
	newHooks := make([]ResponseOption, 0)
	newHooks = append(newHooks, func(hook interface{}) {
		response := hook.(*CommonResponse)
		response.Code = 0
		response.Message = "SUCCESS"
	})
	newHooks = append(newHooks, hooks...)
	return Custom(newHooks...)
}

func Fail(hooks ...ResponseOption) *CommonResponse {
	newHooks := make([]ResponseOption, 0)
	newHooks = append(newHooks, func(hook interface{}) {
		response := hook.(*CommonResponse)
		response.Code = 1
		response.Message = "Fail"
	})
	newHooks = append(newHooks, hooks...)
	return Custom(newHooks...)
}

func Custom(hooks ...ResponseOption) *CommonResponse {
	option := new(CommonResponse)
	for _, hook := range hooks {
		hook(option)
	}
	return option
}
